-- HolyShrine
-- Created by chengb Aug/4/2016
-- 神圣祠堂

require "game/ui/form/dungeon/special/UIHolyShrine"

-- 初始化
function initHolyShrine(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");
            if type(elementDbase["lock_id"]) == 'number' then
                if not BraveVSDragonM.isBuidingUnlock(elementDbase["lock_id"]) then
                    -- 建筑未解锁
                    alert(getLocStr("cannot_arrive"));
                    return;
                end
            end

            if type(elementDbase["require_equip"]) == 'number' then
                if not EquipM.isEquiped(elementDbase["require_equip"]) then
                    -- 未穿戴指定装备
                    self:openBuildingView(nil, nil, buildingInfo["dialog2"], buildingInfo["view_text2"]);
                    return;
                end
            end

            -- 打开神圣祠堂界面
            AudioM.playFx("ui_open1");

            local submitList = FormulaM.invoke("CALC_HOLY_SHRINE_COST");

            assert(type(submitList) == 'table', "必须配置提交列表");

            local uiHolyShrine = UIHolyShrine.create(self.gridData, submitList);
            UIMgr.getCurrentScene():removeFormByName("UIHolyShrine");
            UIMgr.getCurrentScene():addForm(uiHolyShrine);
        end
    end

    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setVisible(true);

    if BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
        -- 已经领取过建筑奖励了，格子隐藏
        self.icon:setVisible(false);
    end
end

-- 神圣祠堂提交的回调
function onHolyShrineSubmited(self, bonus)
    local tip = BuildingBonusM.getBuildingBonusTip(self.gridData.class);
    local alertList = splitString(tip, "\n");
    splitAlert(alertList);

    self:playBonusListEffect(bonus);

    local function callback()
        playBuildingDisappear(self);
    end

    performWithDelay(self, callback, 0.5);
end

